home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Acere (Card Game)
/
AcereÄ.sit
/
Acereƒ
/
CardWell.cp
< prev
next >
Wrap
Text File
|
1994-08-25
|
9KB
|
357 lines
// ===========================================================================
// CardWell.cp ⌐1993 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// Class for an object that can draw itself and respond to mouse clicks
#ifdef PowerPlant_PCH
#include PowerPlant_PCH
#endif
#include "CardWell.h"
#include "CardDeck.h"
#include "WellDeck.h"
#include "CTextDoc.h"
#include "AcereApp.h"
#include <LView.h>
#include <LStream.h>
#include <PP_Messages.h>
#include <UDrawingState.h>
extern CTextDoc *theDoc;
// ---------------------------------------------------------------------------
// Ñ CreatePaneStream [static]
// ---------------------------------------------------------------------------
// Return a new Pane object initialized using data from a Stream
CardWell*
CardWell::CreateCardWell(
LStream *inStream)
{
return (new CardWell(inStream));
}
// ---------------------------------------------------------------------------
// Ñ CardWell()
// ---------------------------------------------------------------------------
// Default Constructor
CardWell::CardWell()
{
mPaneID = 0;
mFrameSize.width = mFrameSize.height = 0;
mFrameLocation.h = mFrameLocation.v = 0;
mUserCon = 0;
mFrameBinding.left =
mFrameBinding.top =
mFrameBinding.right =
mFrameBinding.bottom = false;
mVisible = mActive = mEnabled = triState_Latent;
mSuperView = nil;
}
// ---------------------------------------------------------------------------
// Ñ CardWell(const CardWell&)
// ---------------------------------------------------------------------------
// Copy Constructor
CardWell::CardWell(
const CardWell &inOriginal)
{
// Copy members of Original
mPaneID = inOriginal.mPaneID;
mFrameSize = inOriginal.mFrameSize;
mFrameLocation = inOriginal.mFrameLocation;
mFrameBinding = inOriginal.mFrameBinding;
mUserCon = inOriginal.mUserCon;
mSuperView = nil; // Copy is not inside any View
// Pane properties. If Original has
// property ON, Copy is Latent.
mVisible = inOriginal.mVisible;
if (mVisible == triState_On) {
mVisible = triState_Latent;
}
mActive = inOriginal.mActive;
if (mActive == triState_On) {
mActive = triState_Latent;
}
mEnabled = inOriginal.mEnabled;
if (mEnabled == triState_On) {
mEnabled = triState_Latent;
}
}
// ---------------------------------------------------------------------------
// Ñ CardWell(SPaneInfo&)
// ---------------------------------------------------------------------------
// Construct Pane from data in a struct
CardWell::CardWell(
const SPaneInfo &inPaneInfo)
{
InitPane(inPaneInfo);
}
// ---------------------------------------------------------------------------
// Ñ CardWell(LStream*)
// ---------------------------------------------------------------------------
// Construct Pane from data in a Stream
CardWell::CardWell(
LStream *inStream)
{
SPaneInfo thePaneInfo;
inStream->ReadData(&thePaneInfo, sizeof(SPaneInfo));
InitPane(thePaneInfo);
}
// ---------------------------------------------------------------------------
// Ñ InitPane
// ---------------------------------------------------------------------------
// Initialize Pane from data in a struct
void
CardWell::InitPane(
const SPaneInfo &inPaneInfo)
{
short whichCard;
mPaneID = inPaneInfo.paneID;
mFrameSize.width = inPaneInfo.width;
mFrameSize.height = inPaneInfo.height;
mUserCon = inPaneInfo.userCon;
mVisible = triState_Off;
if (inPaneInfo.visible) {
mVisible = triState_Latent;
}
mActive = triState_Latent;
mEnabled = triState_Off;
if (inPaneInfo.enabled) {
mEnabled = triState_Latent;
}
mFrameBinding = inPaneInfo.bindings;
mSuperView = nil;
LView *theSuperView = inPaneInfo.superView;
if (theSuperView == Default_SuperView) {
theSuperView = GetDefaultView();
}
PutInside(theSuperView);
if (theSuperView != nil) {
PlaceInSuperImageAt(inPaneInfo.left, inPaneInfo.top, false);
Boolean expandHoriz = (inPaneInfo.width < 0);
Boolean expandVert = (inPaneInfo.height < 0);
if (expandHoriz || expandVert) {
theSuperView->ExpandSubPane(this, expandHoriz, expandVert);
}
}
whichCard = theDeck->GetNextCardPosition();
theDeck->GetCardInfo(whichCard, &itsCard);
itsCard.card = kNoCard;
}
// ---------------------------------------------------------------------------
// Ñ ~CardWell
// ---------------------------------------------------------------------------
// Destructor
CardWell::~CardWell()
{
PutInside(nil);
if (sLastPaneClicked == this) {
sLastPaneClicked = nil;
}
}
// ---------------------------------------------------------------------------
// Ñ Draw
// ---------------------------------------------------------------------------
// Try to draw contents of a Pane
//
// inSuperDrawRgnH specifies, in Port coordinates, the portion of the
// Pane's SuperView that needs to be drawn. Specify nil to bypass
// the intersection test.
//
// This is a wrapper function which calls DrawSelf if it is proper for
// the Pane to draw. This means that:
// > Pane's Visible property is on
// > Pane can be focused
// > Pane's Frame is in QuickDraw space
// > Pane's Frame intersects inSuperDrawRgnH
void CardWell::Draw(RgnHandle inSuperDrawRgnH)
{
Rect frame;
if ( IsVisible() &&
FocusDraw() &&
CalcPortFrameRect(frame) &&
((inSuperDrawRgnH == nil) || RectInRgn(&frame, inSuperDrawRgnH)) )
{
if (itsCard.card == kNoCard)
{
frame.bottom = frame.top + 90;
EraseRect(&frame);
FrameRoundRect(&frame, 20, 20);
}
else
{
theDeck->DrawCard(&itsCard, frame);
// FrameRoundRect(&frame, 20, 20);
}
}
}
Boolean CardWell::CanRemove(void) // can we remove cards from this pile?
{
return (true);
}
Boolean CardWell::CanDrop(CardStruct *draggedCard)
{
if (itsCard.card == kNoCard)
return (CanDropOnEmptySlot(draggedCard));
else
return (CanDropOnSlot(draggedCard));
}
Boolean CardWell::CanDropOnEmptySlot(CardStruct *draggedCard)
{
// this will be overridden by most subclasses, which is why it's separate
// default behavior is that ANY card can be dropped on an empty slot
return (true);
}
Boolean CardWell::CanDropOnSlot(CardStruct *draggedCard)
{
// this will be overridden by most subclasses.
// default behavior is that no card can be dropped on any card.
return (false);
}
void CardWell::AddCardToWell(CardWell *whichWell, CardStruct *whichCard)
{
whichCard->itsOwner = this;
itsCard = *whichCard;
}
void CardWell::RemoveCardFromWell(CardWell *whichWell, CardStruct *whichCard)
{
itsCard.card = kNoCard;
}
void CardWell::Click(SMouseDownEvent &inMouseDown)
{
CardWell *firstOwner, *secondOwner;
LPane::Click(inMouseDown);
if (sClickCount > 1) // user double-clicked
{
short j;
for (j=0; j < 4; j++)
{
secondOwner = theDoc->theDeckWells[j]->itsCard.itsOwner;
if (secondOwner->CanDrop(&(theDoc->firstCard)))
{
firstOwner = theDoc->firstCard.itsOwner;
theDoc->firstCard.itsOwner->RemoveCardFromWell(theDoc->firstCard.itsOwner,
&theDoc->firstCard);
theDoc->theDeckWells[j]->itsCard.itsOwner->AddCardToWell(
theDoc->theDeckWells[j]->itsCard.itsOwner,
&theDoc->firstCard);
theDoc->firstCard.card = theDoc->secondCard.card = kNoCard;
firstOwner->Draw(nil);
secondOwner->Draw(nil);
return;
}
}
}
if (theDoc->firstCard.card == kNoCard)
{
if (CanRemove())
theDoc->firstCard = itsCard;
else
SysBeep(3);
}
else if ((theDoc->firstCard.card == itsCard.card) &&
(theDoc->firstCard.suit == itsCard.suit))
theDoc->firstCard.card = kNoCard; // toggle selection
else
{
theDoc->secondCard = itsCard;
// this is a little tricky -- we need to call the subclass's method
if (theDoc->secondCard.itsOwner->CanDrop(&(theDoc->firstCard)))
{
// we can drag
// first we remove the card from its old slot
firstOwner = theDoc->firstCard.itsOwner;
secondOwner = theDoc->secondCard.itsOwner;
theDoc->firstCard.itsOwner->RemoveCardFromWell(theDoc->firstCard.itsOwner,
&theDoc->firstCard);
// then we add it to the second slot
theDoc->secondCard.itsOwner->AddCardToWell(theDoc->secondCard.itsOwner,
&theDoc->firstCard);
theDoc->firstCard.card = theDoc->secondCard.card = kNoCard;
firstOwner->Draw(nil);
secondOwner->Draw(nil);
}
else // we couldn't drop the card
{
theDoc->secondCard.card = kNoCard;
SysBeep(3);
}
}
}